import ImageCompressor from 'js-image-compressor';
import {
  baseService
} from '@/service/common/baseService.js';
import {
  ToastPlugin
} from 'vux';
import Vue from 'vue'

Vue.use(ToastPlugin)

/**
 * 压缩图片
 * file {Object File} file对象
 * decrement {Number} 压缩级别 0 - 1
 */
function ImageCompress(file, decrement = 0.2) {
  return new Promise((resolve, reject) => {
    try {
      new ImageCompressor({
        file,
        quality: decrement,
        success: (result) => {
          resolve(result)
        }
      });
    } catch (error) {
      reject(error)
    }
  })
}

/**
 * 图片压缩
 * @param file
 * @returns {Promise<unknown>}
 */
function compressionImage(file) {
  return new Promise((resolve, reject) => {
    // eslint-disable-next-line no-new
    new ImageCompressor({
      file: file,
      quality: 0.1,
      maxWidth: 4000,
      maxHeight: 2000,
      redressOrientation: false,

      // 压缩前回调
      beforeCompress: function (result) {
        console.log('压缩之前图片尺寸大小: ', result.size)
        console.log('mime 类型: ', result.type)
      },

      success: function (result) {
        console.log('压缩之后图片尺寸大小: ', result.size)
        console.log('mime 类型: ', result.type)
        console.log('实际压缩率： ', ((file.size - result.size) / file.size * 100).toFixed(2) + '%')
        resolve(result)
      },
      error(e) {
        reject(e)
      }
    })
  })
}

/**
 * 上传图片
 */
function UploadImage(base64Strs) {
  return new Promise((resolve, reject) => {
    try {
      let base64Strings = base64Strs
      let upload = baseService.post(Vue.__ctx + '/image/upload/base64', base64Strings)
      upload.then(res => {
        if (res.isOk) {
          resolve(res.data)
        } else {
          Vue.$vux.toast.show({
            text: '上传失败！',
            type: 'text',
            time: 1500,
            position: 'middle'
          })
          reject(error)
        }
      }).catch(error => {
        reject(error)
      })
    } catch (error) {
      reject(error)
    }
  })
}

/**
 * OCR上传
 */
function UploadOCR(base64Strs) {
  return new Promise((resolve, reject) => {
    try {
      let base64Strings = base64Strs
      let upload = baseService.postForm(Vue.__ctx + '/facePlusPlus/tool/ocr/idCard', base64Strings)
      upload.then(res => {
        if (res.isOk) {
          resolve(res)
        } else {
          // Vue.$vux.toast.show({
          //  text: res.msg,
          //  type: 'warn',
          // })
          reject(res.msg)
        }
      }).catch(error => {
        // Vue.$vux.toast.show({
        //   text: '识别失败，请重新上传',
        //   type: 'warn'
        // })
        reject('识别失败，请重新上传')
      })
    } catch (error) {
      // Vue.$vux.toast.show({
      //   text: '识别失败，请重新上传',
      //   type: 'warn'
      // })
      reject('识别失败，请重新上传')
    }
  })
}

/**
 * OCR 营业执照上传
 */
function UploadYyzzOCR(base64Strs) {
  return new Promise((resolve, reject) => {
    try {
      let base64Strings = base64Strs
      let upload = baseService.postForm(Vue.__ctx + '/facePlusPlus/tool/ocr/businessLicense', base64Strings)
      upload.then(res => {
        if (res.isOk) {
          resolve(res)
        } else {
          // Vue.$Message.error(res.msg)
          reject('识别失败，请重新上传')
        }
      }).catch(error => {
        // debugger
        // Vue.$Message.error('识别失败，请重新上传')
        reject('识别失败，请重新上传')
      })
    } catch (error) {
      // Vue.$Message.error('识别失败，请重新上传')
      reject('识别失败，请重新上传')
    }
  })
}

export default {
  ImageCompress,
  compressionImage,
  UploadImage,
  UploadOCR,
  UploadYyzzOCR
}
